package com.ttscm.srds.device.modbus;

import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
import com.ttscm.srds.config.DeviceParam;
import com.ttscm.srds.utils.ConnectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.UnknownHostException;

/**
 * @version:
 * @author:
 * @className:
 * @packageName:
 * @description: modbus设备读写
 * @date: 2022/07/28
 */
@Component
public class DeviceHsj {
    @Autowired
    private DeviceParam config;


    public void setSignal(int startAddress, int value) throws ModbusProtocolException, UnknownHostException, ModbusNumberException, ModbusIOException {

        ConnectionUtil.writeModbusRegisters(config.getHost(), config.getPort(), config.getSlaveId(), startAddress, new int[]{value});
    }

    public int readSignal(int startAddress) throws ModbusProtocolException, UnknownHostException, ModbusNumberException, ModbusIOException {
        int[] signal = ConnectionUtil.readModbusHoldingRegisters(config.getHost(), config.getPort(), config.getSlaveId(), startAddress, 1);

        return signal[0];
    }

    /**
     * 通知线边设备
     * <p>
     * 0-初始
     * 1-满蛋车
     * 2-空蛋车
     *
     * @throws ModbusProtocolException
     * @throws UnknownHostException
     * @throws ModbusNumberException
     * @throws ModbusIOException
     */
    public void setSignal() throws ModbusProtocolException, UnknownHostException, ModbusNumberException, ModbusIOException {

        ConnectionUtil.writeModbusRegisters(config.getHost(), config.getPort(), config.getSlaveId(), 0, new int[]{2});
    }

    /**
     * 重置信号
     * <p>
     * 0-初始
     * 1-满蛋车
     * 2-空蛋车
     *
     * @throws ModbusProtocolException
     * @throws UnknownHostException
     * @throws ModbusNumberException
     * @throws ModbusIOException
     */
    public void resetSignal() throws ModbusProtocolException, UnknownHostException, ModbusNumberException, ModbusIOException {

        ConnectionUtil.writeModbusRegisters(config.getHost(), config.getPort(), config.getSlaveId(), 0, new int[]{0});
    }

    /**
     * 读取线边设备
     * <p>
     * 0-初始
     * 1-满蛋车
     * 2-空蛋车
     *
     * @return
     * @throws ModbusProtocolException
     * @throws UnknownHostException
     * @throws ModbusNumberException
     * @throws ModbusIOException
     */
    public int readSignal() throws ModbusProtocolException, UnknownHostException, ModbusNumberException, ModbusIOException {
        int[] signal = ConnectionUtil.readModbusHoldingRegisters(config.getHost(), config.getPort(), config.getSlaveId(), 0, 1);

        return signal[0];
    }

    /**
     * 通知线边设备
     * <p>
     * 0-初始
     * 1-满蛋车
     * 2-空蛋车
     */
    public void setSignalS7() {

        ConnectionUtil.writeSiemensPlcS7(config.getHost(), config.getPort(), config.getAddress(), 1);
    }

    /**
     * 重置信号
     * <p>
     * 0-初始
     * 1-满蛋车
     * 2-空蛋车
     */
    public void resetSignalS7() {
        ConnectionUtil.writeSiemensPlcS7(config.getHost(), config.getPort(), config.getAddress(), 0);
    }

    /**
     * 读取线边设备
     * <p>
     * 0-初始
     * 1-满蛋车
     * 2-空蛋车
     */
    public int readSignalS7() {
        int signal = ConnectionUtil.readSiemensPlcS7(config.getHost(), config.getPort(), config.getAddress());

        return signal;
    }
}
